package com.ruoyi.orders.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.orders.domain.Orders;
import com.ruoyi.orders.service.IOrdersService;
import com.ruoyi.medication.domain.MedicationStock;
import com.ruoyi.medication.service.IMedicationStockService;
import com.ruoyi.product.service.ISupplierProductService;
import com.ruoyi.product.domain.SupplierProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 药品采购订单Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/orders/orders")
public class OrdersController extends BaseController {
    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IMedicationStockService medicationStockService;

    @Autowired
    private ISupplierProductService supplierProductService;

    /**
     * 查询药品采购订单列表
     */
    @PreAuthorize("@ss.hasPermi('orders:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(Orders orders) {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }

    /**
     * 导出药品采购订单列表
     */
    @PreAuthorize("@ss.hasPermi('orders:orders:export')")
    @Log(title = "药品采购订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders) {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.exportExcel(response, list, "药品采购订单数据");
    }

    /**
     * 获取药品采购订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('orders:orders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ordersService.selectOrdersById(id));
    }

    /**
     * 新增药品采购订单
     */
    @PreAuthorize("@ss.hasPermi('orders:orders:add')")
    @Log(title = "药品采购订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders) {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改药品采购订单
     */
    @PreAuthorize("@ss.hasPermi('orders:orders:edit')")
    @Log(title = "药品采购订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders) {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除药品采购订单
     */
    @PreAuthorize("@ss.hasPermi('orders:orders:remove')")
    @Log(title = "药品采购订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ordersService.deleteOrdersByIds(ids));
    }

    /**
     * 获取药品单价
     */
    @PreAuthorize("@ss.hasPermi('orders:orders:query')")
    @GetMapping("/getMedicationPrice")
    public AjaxResult getMedicationPrice(@RequestParam Long medicationId, @RequestParam Long supplierId) {
        if (medicationId == null || supplierId == null) {
            return error("药品ID和供应商ID不能为空");
        }

        SupplierProduct supplierProduct = supplierProductService
                .selectSupplierProductBySupplierAndMedication(supplierId, medicationId);
        if (supplierProduct != null) {
            return success(supplierProduct.getPrice());
        }
        return error("未找到供应商[" + supplierId + "]提供的药品[" + medicationId + "]的价格信息");
    }
}
